//
//  SEGAwardAdView.m
//  ExcellentHome
//
//  Created by 揭婧雯 on 16/11/30.
//  Copyright © 2016年 陈区. All rights reserved.
//

#import "SEGAwardAdView.h"

@implementation SEGAwardAdView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self customUI];
    }
    return self;
}

#pragma mark - 初始化界面

- (void)customUI
{
    self.backgroundColor = kColor_bg3;

    //白色背景视图
    self.contentView = [[UIView alloc] init];
    self.contentView.backgroundColor = [UIColor whiteColor];
    self.contentView.layer.masksToBounds = YES;
    self.contentView.layer.cornerRadius = 5;
    [self addSubview:self.contentView];
    //SAM:iOS10兼容-待测试
    [self.contentView mas_makeConstraints:^(MASConstraintMaker* make) {
        make.centerX.equalTo(self.mas_centerX);
        make.centerY.equalTo(self.mas_centerY).with.offset(-50);
        make.width.mas_equalTo(270);
        make.height.mas_equalTo(231);
    }];

    //关闭按钮
    self.closeButton = [[UIButton alloc] init];
    self.closeButton.backgroundColor = [UIColor clearColor];
    [self.closeButton setImage:[UIImage imageWithContentsOfFile:SEGPicName(@"btn_dialog_close_gray")] forState:UIControlStateNormal];
    [self.closeButton addTarget:self action:@selector(closeButtonClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.contentView addSubview:self.closeButton];
    [self.closeButton mas_makeConstraints:^(MASConstraintMaker* make) {
        make.top.equalTo(self.contentView.mas_top);
        make.right.equalTo(self.contentView.mas_right);
        make.width.mas_equalTo(35);
        make.height.mas_equalTo(35);
    }];

    //标题
    self.titleLabel = [[UILabel alloc] init];
    self.titleLabel.text = @"精心推荐";
    self.titleLabel.font = [UIFont systemFontOfSize:13];
    self.titleLabel.textColor = kColor_3;
    self.titleLabel.backgroundColor = [UIColor clearColor];
    [self.contentView addSubview:self.titleLabel];
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker* make) {
        make.centerX.equalTo(self.contentView.mas_centerX);
        make.top.equalTo(self.contentView.mas_top).with.offset(40);
        make.width.mas_greaterThanOrEqualTo(0);
        make.height.mas_equalTo(13);
    }];

    //标题左右横线
    self.leftLine = [[UIView alloc] init];
    self.leftLine.backgroundColor = kColor_L2;
    [self.contentView addSubview:self.leftLine];
    [self.leftLine mas_makeConstraints:^(MASConstraintMaker* make) {
        make.left.equalTo(self.contentView.mas_left).with.offset(15);
        make.right.equalTo(self.titleLabel.mas_left).with.offset(-15);
        make.centerY.equalTo(self.titleLabel.mas_centerY);
        make.height.mas_equalTo(0.5);
    }];
    self.rightLine = [[UIView alloc] init];
    self.rightLine.backgroundColor = kColor_L2;
    [self.contentView addSubview:self.rightLine];
    [self.rightLine mas_makeConstraints:^(MASConstraintMaker* make) {
        make.left.equalTo(self.titleLabel.mas_right).with.offset(15);
        make.right.equalTo(self.contentView.mas_right).with.offset(-15);
        make.centerY.equalTo(self.titleLabel.mas_centerY);
        make.height.mas_equalTo(0.5);
    }];

    //图片
    self.iconView = [[UIImageView alloc] init];
    self.iconView.backgroundColor = [UIColor clearColor];
    [self.contentView addSubview:self.iconView];
    [self.iconView mas_makeConstraints:^(MASConstraintMaker* make) {
        make.left.equalTo(self.contentView.mas_left).with.offset(15);
        make.top.equalTo(self.titleLabel.mas_bottom).with.offset(30);
        make.width.mas_equalTo(85);
        make.height.mas_equalTo(60);
    }];

    //商品名称
    self.nameLabel = [[UILabel alloc] init];
    //    self.nameLabel.text = @"nameLabelnameLabelnameLab";
    self.nameLabel.font = [UIFont systemFontOfSize:13];
    self.nameLabel.textColor = kColor_2;
    self.nameLabel.numberOfLines = 2;
    self.nameLabel.backgroundColor = [UIColor clearColor];
    [self.contentView addSubview:self.nameLabel];
    [self.nameLabel mas_makeConstraints:^(MASConstraintMaker* make) {
        make.left.equalTo(self.iconView.mas_right).with.offset(10);
        make.top.equalTo(self.titleLabel.mas_bottom).with.offset(32);
        make.right.equalTo(self.contentView.mas_right).with.offset(-15);
        make.height.mas_greaterThanOrEqualTo(13);
    }];

    //商品价格
    self.priceLabel = [[UILabel alloc] init];
    //    self.priceLabel.text = @"60.50";
    self.priceLabel.font = [UIFont systemFontOfSize:13];
    self.priceLabel.textColor = kColor_S1;
    self.priceLabel.backgroundColor = [UIColor clearColor];
    [self.contentView addSubview:self.priceLabel];
    [self.priceLabel mas_makeConstraints:^(MASConstraintMaker* make) {
        make.left.equalTo(self.iconView.mas_right).with.offset(10);
        make.bottom.equalTo(self.iconView.mas_bottom);
        make.right.equalTo(self.contentView.mas_right).with.offset(-15);
        make.height.mas_greaterThanOrEqualTo(13);
    }];

    //跳转按钮
    self.goButton = [[UIButton alloc] init];
    self.goButton.layer.masksToBounds = YES;
    self.goButton.layer.cornerRadius = 3;
    [self.goButton setTitle:@"去看看" forState:UIControlStateNormal];
    [self.goButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [self.goButton addTarget:self action:@selector(goButtonClick:) forControlEvents:UIControlEventTouchUpInside];
    self.goButton.titleLabel.font = [UIFont systemFontOfSize:16];
    self.goButton.backgroundColor = kColor_T;
    [self.goButton setBackgroundImage:[UIImage imageWithColor:kColor_T] forState:UIControlStateNormal];
    [self.goButton setBackgroundImage:[UIImage imageWithColor:kColor_T_Press] forState:UIControlStateHighlighted];
    [self.contentView addSubview:self.goButton];
    [self.goButton mas_makeConstraints:^(MASConstraintMaker* make) {
        make.centerX.equalTo(self.contentView.mas_centerX);
        make.bottom.equalTo(self.contentView.mas_bottom).with.offset(-15);
        make.width.mas_equalTo(240);
        make.height.mas_equalTo(44);
    }];
}

- (void)closeButtonClick:(UIButton*)sender
{
    [self removeFromSuperview];
}

- (void)goButtonClick:(UIButton*)sender
{
    //把门禁列表界面消失
//    [self.navigat dismissViewControllerAnimated:NO completion:nil];
    
    if ([self.prizeType intValue] == kPrizeTypeHomeService) { //到家服务
        NSDictionary* dic = [NSDictionary dictionaryWithObjectsAndKeys:
                             @"cn.segi.uhome.action.HOME_SERVICE_DETAIL", @"action", self.goUrlString, @"detailUrl", nil];
        [[SEGUhomeMediator sharedInstance]seg_pushToViewControllerWithAction:dic];
    }
    else {
        NSArray* arr = [self.goUrlString componentsSeparatedByString:@"#"];
        NSDictionary* dic = [NSDictionary dictionaryWithObjectsAndKeys:
                             arr[0], @"action", arr[1], @"serviceId", nil];
        [[SEGUhomeMediator sharedInstance]seg_pushToViewControllerWithAction:dic];
    }
    
//    [self.delegate clickGoButtonWithUrlString:self.goUrlString prizeType:[self.prizeType intValue]];
    if (self.callback) {
        self.callback();
    }
    [self removeFromSuperview];
}

- (void)updateViewWithDict:(NSDictionary*)dict callBack:(SEGAwardADViewCallBack)callback
{
    self.callback = callback;
    [self.iconView setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@/%@", kClientImageRootURL, [dict objectForKey:@"prizeIcon"]]]];
    self.nameLabel.text = [dict objectForKey:@"prizeName"];
    NSString* price = [dict objectForKey:@"price"];
    if (price) {
        self.priceLabel.text = [NSString stringWithFormat:@"%.2f 元", [price floatValue] / 100];
    }
    self.goUrlString = [dict objectForKey:@"forwardUrl"];
    self.prizeType = [dict objectForKey:@"prizeType"];
}

@end
